//
//  DataBaseHandler.m
//  livingDesign
//
//  Created by dlios on 15-2-2.
//  Copyright (c) 2015年 怡居. All rights reserved.
//

#import "DataBaseHandler.h"
#import "DBModel.h"


@implementation DataBaseHandler

+ (DataBaseHandler *)shareInstance
{
    static DataBaseHandler *dataHandler = nil;
    if (dataHandler == nil) {
        dataHandler = [[DataBaseHandler alloc] init];
        [dataHandler openDB];
        [dataHandler createDB];
    }
    return dataHandler;
}
- (void)openDB
{
    // 获取doucuments文件夹路径
    NSString *docPsth = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 拼接一个文件路径
    NSString *dbPath = [docPsth stringByAppendingString:@"livingDesign.db"];
    
    
    
    // UTF8String   将一个字符串转换成char*类型
    // 参数1：本地数据库文件存储的路径
    // 参数2：数据库的指针地址
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    
    [self judgeResult:result name:@"打开"];
    
    
    NSLog(@"数据库地址%@", dbPath);
    NSLog(@"打开结果%d", result);   // 0为成功
}
- (void)createDB
{
    NSString *SQL = [NSString stringWithFormat:@"create table livingDesign (albumId text primary key, title text, img text, benDiDressURL text)"];
    
    // 参数1：数据库指针
    // 参数2：SQL语句
    
    int result = sqlite3_exec(dbPoint, [SQL UTF8String], NULL, NULL, NULL);
    [self judgeResult:result name:@"创建表"];
}
- (void)insertDataWithNewest:(Newest *)newest newestDetail:(NSString *)newestDetailURL
{
    NSString *insertStr = [NSString stringWithFormat:@"insert into livingDesign values('%@', '%@', '%@', '%@' )", newest.albumId, newest.title, newest.img, newestDetailURL];
    int result = sqlite3_exec(dbPoint, [insertStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result name:@"插入"];
}

// 查找数据
- (NSMutableArray *)selectDB
{
    // 查询数据
    // 1.创建一个数据库状态指针 （临时的数据库， 保存所有对数据库的更改和操作， 结束后统一写入本地）；
    sqlite3_stmt *stmt = nil;
    
    // 2.stmt指针指向本地数据库
    
    // 参数1. 数据库指针
    // 参数2. SQL语句
    // 参数3. 限制SQL语句的长度
    // 参数4. stmt的指针地址
    // 作用1. 把stmt指针指向本地数据，获取了本地一部分数据
    //    2. 就是将SQL语句执行的结果放在了临时数据库里面
    int result = sqlite3_prepare_v2(dbPoint, "select * from livingDesign", -1, &stmt, NULL);
    
    // 3.把有用的数据从stmt中 提取出来
    
    // 创建一个空得可变数组
    NSMutableArray *arr = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 提取每一行的数据
            
            // 参数1. stmt指针
            // 参数2. 获取第几列的数据
            const unsigned char *albumIdChar = sqlite3_column_text(stmt, 0);
            NSString *albumId = [NSString stringWithUTF8String:(const char*)albumIdChar];
            
            //(albumId text primary key, title text, img text, benDiDressURL text
            const unsigned char *titleChar = sqlite3_column_text(stmt, 1);
            NSString *title = [NSString stringWithUTF8String:(const char*)titleChar];
            
            
            const unsigned char *imgChar = sqlite3_column_text(stmt, 2);
            NSString *img = [NSString stringWithUTF8String:(const char*)imgChar];
            
            const unsigned char *benDiDressURLChar = sqlite3_column_text(stmt, 3);
            NSString *benDiDressURL = [NSString stringWithUTF8String:(const char*)benDiDressURLChar];
            
            
            
            // 将数据赋值到model类中
            DBModel *dbModel = [[DBModel alloc] init];
            dbModel.title =title;
            dbModel.albumId = albumId;
            dbModel.img = img;
            dbModel.benDiDressURL = benDiDressURL;
            
            [arr addObject:dbModel];
            
            
        }
    }
    
    // 清空
    sqlite3_finalize(stmt);
    
    
    return  arr;
    
    
}

// 删除
- (void)deleteDBModel:(NSString *)albumId
{
    NSString *deleteStr = [NSString stringWithFormat:@"delete from livingDesign where %@", albumId];
    int result = sqlite3_exec(dbPoint, [deleteStr UTF8String], NULL, NULL, NULL);
     [self judgeResult:result name:@"删除"];
}



// 判定结果的打印

- (void)judgeResult:(int)result name:(NSString *)name
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", name);
    }else
    {
        NSLog(@"%@失败", name);
    }
}
@end
